package com.xmkj.bearbiz.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.xmkj.bearbiz.R;
import com.xmkj.bearbiz.bean.OneBean;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/8/1.
 */

public class ZheJieStarAdapter extends RecyclerView.Adapter<ZheJieStarAdapter.ViewHolder> {

    Context context;
    ArrayList<OneBean> list;


    public ZheJieStarAdapter(Context context, ArrayList<OneBean> datas) {
        this.context = context;
        this.list = datas;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        View view = View.inflate(context, R.layout.mine_tab, null);
        View view = LayoutInflater.from(context).inflate(R.layout.item_star, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        if(list.get(position).isjuade){
            holder.ivImg.setBackgroundResource(R.mipmap.star_true);
        }else {
            holder.ivImg.setBackgroundResource(R.mipmap.star_false);
        }
        holder.ivImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickListener.zhejieclick(position);
                changeUi(position);

            }
        });


    }

    private void changeUi(int position) {
        for (int i = 0; i < 5; i++) {
            OneBean starBean = new OneBean() ;
            if(i<= position){
                starBean.isjuade = true;
               list.set(i,starBean);
            }else {
                starBean.isjuade = false;
                list.set(i,starBean);
            }
        }
        notifyDataSetChanged();
    }

    private ClickListener clickListener;

    public void setItemClick(ClickListener click) {
        this.clickListener = click;
    }

    public interface ClickListener {
        void zhejieclick(int position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivImg;
        View main;

        public ViewHolder(View itemView) {
            super(itemView);
            main = itemView;
            ivImg = (ImageView) itemView.findViewById(R.id.iv_pic);
        }
    }
}


